﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wyn.PMS.Common.SecurityControl;
using Wyn.PMS.Common.Utils;
using Wyn.PMS.Entity.Entities;

namespace Wyn.PMS.Common.NightAudit
{
    
    public class CodeHelper
    {
        public const string BASE_PARENT_KEY = "0";

        private static readonly object locker = new object();

        private static volatile IDictionary<string, string> codeParentKeyDict;
        
        public static IEnumerable<DictKeyValue> GetBizCode()
        {
            return BizCodeCache.GetBizCode(GlobalConst.ROOT_HOTEL_ID);
        }

        public static IEnumerable<DictKeyValue> GetBizCode(string parentKey)
        {
            return BizCodeCache.GetBizCode(GlobalConst.ROOT_HOTEL_ID, parentKey);
        }

        public static string GetKeyName(string parentKey, string keyValue)
        {
            var code =BizCodeCache.GetKeyName(GlobalConst.ROOT_HOTEL_ID, parentKey, keyValue);

            if (code != null) return code.KeyName;

            return string.Empty;
        }

        public static string GetKeyValue(string parentKey, string keyName)
        {
            var code = BizCodeCache.GetKeyValue(GlobalConst.ROOT_HOTEL_ID, parentKey, keyName);

            if (code != null) return code.KeyValue;

            return string.Empty;
        }

       
        public static string GetName<T>(T t) where T : struct, IConvertible
        {
            string name = string.Empty;

            try
            {
                string parentKey = GetParentKey(typeof(T).Name);

                string value = EnumHelper.GetCode<T>(t);

                name= GetKeyName(parentKey, value);

            }catch(Exception error)
            {
                OperateLog.ErrorLog(error);
            }

            return name;
        }

   
        public static string GetName<T>(string code) where T : struct, IConvertible
        {
            if (!typeof(T).IsEnum)
            {
                throw new ArgumentException("T must be an enumerated type");
            }

            T t= EnumHelper.GetValue<T>(code);

            return GetName(t);
        }

        public static IEnumerable<DictKeyValue> GetBizCode<T>()
        {
            IEnumerable<DictKeyValue> codes = null;

            try
            {
                string parentKey = GetParentKey(typeof (T).Name);

                codes = BizCodeCache.GetBizCode(GlobalConst.ROOT_HOTEL_ID, parentKey);

            }catch(Exception error)
            {
                OperateLog.ErrorLog(error);
            }
            return codes;
        }

        public static string GetParentKey(string typeName)
        {
            var dict = GetCodeParentKeyDict();

            if (dict.ContainsKey(typeName))
            {
                return dict[typeName];
            }

            return string.Empty;
        }

        private static IDictionary<string, string> GetCodeParentKeyDict()
        {
            if (codeParentKeyDict == null)
            {
                lock (locker)
                {
                    if (codeParentKeyDict == null)
                    {
                        codeParentKeyDict = InitCodeParentKeyDict();
                    }
                }

            }
            return codeParentKeyDict;
        }

        private static IDictionary<string, string> InitCodeParentKeyDict()
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();

            dict.Add(typeof (AccountRootType).Name, "ART");

            dict.Add(typeof (CheckInType).Name, "GID");

            dict.Add(typeof (GuestSourceType).Name, "GSC");

            dict.Add(typeof (OrderStatus).Name, "OS");
            
            dict.Add(typeof(RoomStatus).Name, "RS");

            dict.Add(typeof(RoomAttributeCode).Name, "RA");

            dict.Add(typeof(RoomDescriptionCode).Name, "RD");

            return dict;

            #region 码表

           

            #endregion
        }
    }
}
